<?php

namespace app\portal\controller;

use cmf\controller\AdminBaseController;

use think\facade\Db;

class AdminPageController extends AdminBaseController
{

    /**
     * 页面管理
     */
    public function index()
    {
  
		$data = $this->request->param();
		$map=[];
		if(!empty($data['keyword'])){
            $map[]=['post_title','like',"%".$data['keyword']."%"];
        }
		
		$lists = Db::name("portal_post")
            ->where($map)
			->order("create_time desc")
			->paginate(20);
        
        $lists->each(function($v,$k){
			// $v['thumb']=get_upload_path($v['thumb']);
			$userinfo=$this->getUserInfo($v['user_id']);
			
			$v['user_nickname']=$userinfo['user_nickname'];
            return $v;           
        });
        
		
        
        $page = $lists->render();

    	$this->assign('pages', $lists);

    	$this->assign("page", $page);

        return $this->fetch();
    }
	function getUserInfo($uid){
		$map[]=['id','=',$uid];
		$userinfo=Db::name('user')
					->field('id,user_nickname')
					->where($map)
					->find();
		if(!$userinfo){
			$userinfo=['user_nickname'=>''];
		}
		return $userinfo;
	}

    /**
     * 添加页面
     * @adminMenu(
     *     'name'   => '添加页面',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加页面',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        
        return $this->fetch();
    }

    /**
     * 添加页面提交
     * @adminMenu(
     *     'name'   => '添加页面提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加页面提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
		if ($this->request->isPost()) {
			$data = $this->request->param();
			if($data['subject']==''){
				$this->error("标题不能为空！");
			}
			if($data['template']==''){
				$this->error("内容不能为空！");
			}
			$nowtime=time();
            
			$save_data=array(
				"post_type"=>2,
				"post_format"=>1,
				"user_id"=>1,//读取当前登录用户ID
				"create_time"=>$nowtime,
				"update_time"=>$nowtime,
				"published_time"=>$nowtime,
				"post_title"=>$data['subject'],
				"post_title_en"=>$data['subject_en'],
				"post_content"=>$data['template'],
			);
			
			$id = DB::name('portal_post')->insertGetId($save_data);
            if(!$id){
                $this->error("添加失败！");
            }
            

            $this->success("保存成功！");
        }

    }

    /**
     * 编辑页面
     * @adminMenu(
     *     'name'   => '编辑页面',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑页面',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
		$id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('portal_post')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
		
        $this->assign('post', $data);

        return $this->fetch();
    }

    /**
     * 编辑页面提交
     * @adminMenu(
     *     'name'   => '编辑页面提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑页面提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        if ($this->request->isPost()) {
			
			$data = $this->request->param();
			
			
			$save_data=array(
				"id"=>$data['id'],
				"post_title"=>$data['subject'],
				"post_title_en"=>$data['subject_en'],
				"post_content"=>$data['template'],
			);
			// $this->error($save_data);
			$rs = DB::name('portal_post')->update($save_data);
            if($rs===false){
                $this->error("修改失败！");
            }
		
			$this->success("保存成功！");
		}

    }

    /**
     * 删除页面
     * @author    iyting@foxmail.com
     * @adminMenu(
     *     'name'   => '删除页面',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除页面',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
		$id = $this->request->param('id', 0, 'intval');
        
        $rs = DB::name('portal_post')->where("id={$id}")->delete();
        if(!$rs){
            $this->error("删除失败！");
        }
        
        $this->success("删除成功！");
        

    }

}
